package com.spic.business.plan.comunication.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.plan.comunication.domain.PlanCommunicationSupport;
import com.spic.business.plan.comunication.mapper.PlanCommunicationSupportMapper;
import com.spic.business.plan.comunication.service.IPlanCommunicationSupportService;
import com.spic.common.core.exception.ServiceException;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 应急通讯保障Service业务层处理
 *
 * @author spic
 * @date 2021-10-15
 */
@Service
public class PlanCommunicationSupportServiceImpl implements IPlanCommunicationSupportService
{
    private static final Logger log = LoggerFactory.getLogger(PlanCommunicationSupport.class);
    @Autowired
    private PlanCommunicationSupportMapper planCommunicationSupportMapper;

    /**
     * 查询应急通讯保障
     *
     * @param id 应急通讯保障主键
     * @return 应急通讯保障
     */
    @Override
    public PlanCommunicationSupport selectPlanCommunicationSupportById(Long id)
    {
        return planCommunicationSupportMapper.selectPlanCommunicationSupportById(id);
    }

    /**
     * 查询应急通讯保障列表
     *
     * @param planCommunicationSupport 应急通讯保障
     * @return 应急通讯保障
     */
    @Override
    @BusinessDataScope(menuPath = "business/plan/comunication/index")
    public List<PlanCommunicationSupport> selectPlanCommunicationSupportList(PlanCommunicationSupport planCommunicationSupport)
    {
        return planCommunicationSupportMapper.selectPlanCommunicationSupportList(planCommunicationSupport);
    }

    /**
     * 新增应急通讯保障
     *
     * @param planCommunicationSupport 应急通讯保障
     * @return 结果
     */
    @Override
    public int insertPlanCommunicationSupport(PlanCommunicationSupport planCommunicationSupport)
    {
        planCommunicationSupport.setCreateTime(DateUtils.getNowDate());
        return planCommunicationSupportMapper.insertPlanCommunicationSupport(planCommunicationSupport);
    }

    /**
     * 修改应急通讯保障
     *
     * @param planCommunicationSupport 应急通讯保障
     * @return 结果
     */
    @Override
    public int updatePlanCommunicationSupport(PlanCommunicationSupport planCommunicationSupport)
    {
        planCommunicationSupport.setUpdateTime(DateUtils.getNowDate());
        return planCommunicationSupportMapper.updatePlanCommunicationSupport(planCommunicationSupport);
    }

    /**
     * 批量删除应急通讯保障
     *
     * @param ids 需要删除的应急通讯保障主键
     * @return 结果
     */
    @Override
    public int deletePlanCommunicationSupportByIds(Long[] ids)
    {
        return planCommunicationSupportMapper.deletePlanCommunicationSupportByIds(ids);
    }

    /**
     * 删除应急通讯保障信息
     *
     * @param id 应急通讯保障主键
     * @return 结果
     */
    @Override
    public int deletePlanCommunicationSupportById(Long id)
    {
        return planCommunicationSupportMapper.deletePlanCommunicationSupportById(id);
    }

    @Override
    public List<PlanCommunicationSupport> selectPlanCommunicationSupportByIds(List<Long> ids) {
        return planCommunicationSupportMapper.selectBatchIds(ids);
    }

    @Override
    public String importData(List<PlanCommunicationSupport> dataManagementList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(dataManagementList) || dataManagementList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<PlanCommunicationSupport> existList = selectPlanCommunicationSupportList(null);
        for (PlanCommunicationSupport importData : dataManagementList)
        {
            try {

                boolean Flag = false;
                for (PlanCommunicationSupport entry : existList) {
                    if (entry.getOrganizationName().equals(importData.getOrganizationName())) {
                        Flag = true;
                        break;
                    }
                }
                if (!Flag) {
                    insertPlanCommunicationSupport(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getOrganizationName() + " 导入成功");
                } else if (isUpdateSupport) {
                    updatePlanCommunicationSupport(importData);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + importData.getOrganizationName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + importData.getOrganizationName() + " 已存在");
                }
            }catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + importData.getOrganizationName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "数据已导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
